<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
    >
      <el-form-item label="医生姓名" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入医生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作功能 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        min-width="100"
        prop="doctorName"
        label="医生姓名"
      >
        <template slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="detailsFunc(row.doctorName, false)"
            >{{ row.doctorName }}</el-link
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        min-width="100"
        prop="patientName"
        label="患者姓名"
      >
        <template slot-scope="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="detailsFunc(row.patientName, true)"
            >{{ row.patientName }}</el-link
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        min-width="500"
        prop="content"
        label="随访内容"
        show-overflow-tooltip=""
      />
      <el-table-column
        align="center"
        width="200"
        prop="createTime"
        label="随访时间"
      />

      <el-table-column align="center" width="200" label="操作">
        <template slot-scope="{ row, $index }">
          <el-link
            type="primary"
            icon="el-icon-view"
            :underline="false"
            @click="handleCheck(row)"
            >查看</el-link
          >
          <!-- <el-link
            style="margin: 0 10px"
            type="success"
            icon="el-icon-edit"
            :underline="false"
            @click="handleUpdate(row)"
            >修改</el-link
          > -->
          <el-link
            type="danger"
            icon="el-icon-delete"
            :underline="false"
            @click="handleDel(row, $index)"
            v-if="isAdmin"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="tableData.length > 0"
      :total="tableData.length"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="80%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="医生姓名" prop="doctorId">
          <el-select
            v-model="ruleForm.doctorId"
            placeholder="请选择"
            :disabled="disabled"
            :remote-method="getUserList"
            :remote="true"
            filterable
          >
            <el-option
              v-for="item in doctorList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="患者姓名" prop="patientId">
          <el-select
            v-model="ruleForm.patientId"
            placeholder="请选择"
            :disabled="disabled"
            :remote-method="getPatientList"
            filterable
            :remote="true"
          >
            <el-option
              v-for="item in patientList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="随访内容" prop="content">
          <el-input
            v-model="ruleForm.content"
            clearable
            placeholder="请输入患者姓名"
            :disabled="disabled"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          v-if="title != '随访记录详细信息'"
          type="primary"
          @click="submitForm('ruleForm')"
          :loading="btnLoading"
          >确 定</el-button
        >
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 详细信息组件 -->
    <detailsData ref="detailsData" />
  </div>
</template>

<script>
import { addVisit, getVisits, removeVisit, updateVisit } from "../../api/visit";
import { listUser } from "../../api/system/user";
import { getPatientsByDoctorId } from "../../api/patient";
export default {
  data() {
    return {
      isAdmin: localStorage.getItem("role") === "admin",
      // 防多次点击
      btnLoading: false,
      // 是否修改
      disabled: false,
      // 条数
      total: 0,
      // 医生列表
      doctorList: [],
      // 患者列表
      patientList: [],
      // 搜索条件
      queryParams: {
        patientName: undefined,
        doctorName: undefined,
        pageIndex: 1,
        pageSize: 10,
      },
      // 性别下拉
      sexOptions: [
        { label: "男", value: "0" },
        { label: "女", value: "1" },
      ],
      // 表格数据
      tableData: [],
      // 是否显示搜索
      showSearch: true,
      // 表单校验
      rules: {
        doctorId: [
          { required: true, message: "请输入医生姓名", trigger: "blur" },
        ],
        doctorId: [
          { required: true, message: "请输入患者姓名", trigger: "blur" },
        ],
      },
      // 表单数据
      ruleForm: {},
      // 弹窗状态
      dialogVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹窗顶部文本
      title: "",
    };
  },
  methods: {
    getUserList(name) {
      listUser({ name }).then((res) => {
        this.doctorList = res.data.list || [];
      });
    },
    // 获取患者列表
    getPatientList(name) {
      getPatientsByDoctorId({ name }).then((res) => {
        this.patientList = res.data.list || [];
      });
    },
    // 详情
    detailsFunc(name, state) {
      this.$refs.detailsData.dialogVisible = true;
      this.$refs.detailsData.title = name;
      this.$refs.detailsData.state = state;
      this.$refs.detailsData.getList(name);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增
    handleAdd() {
      this.ruleForm = {
        doctorId: undefined,
        doctorId: undefined,
      };
      this.title = "新增随访记录";
      this.dialogVisible = true;
      this.disabled = false;
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return;

        this.btnLoading = true;
        try {
          const actionFn =
            this.title == "新增随访记录" ? this.addFunc : this.editFunc;
          await actionFn(this.ruleForm);

          this.$modal.msgSuccess("操作成功");
          this.getList();
          this.dialogVisible = false;
          this.btnLoading = false;
        } catch (err) {
          this.btnLoading = false;
        }
      });
    },
    // 新增
    async addFunc(ruleForm) {
      ruleForm.createTime = this.parseTime(new Date());
      await addVisit(ruleForm);
      return { code: 200, msg: "新增随访记录成功" };
    },
    // 修改
    async editFunc(ruleForm) {
      await updateVisit(ruleForm);
      return { code: 200, msg: "修改随访记录成功" };
    },
    // 多选
    handleSelectionChange(value) {
      this.selectList = JSON.parse(JSON.stringify(value));
      this.ids = value.map((item) => item.id);
      this.single = value.length !== 1;
      this.multiple = !value.length;
    },
    // 删除
    handleDel(row, index) {
      const ids = row.fid || this.ids;
      this.$modal
        .confirm(`是否确认删除此数据`)
        .then(() => {})
        .then(async () => {
          await removeVisit(row.id);
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 修改
    handleUpdate(row) {
      this.title = "修改随访记录";
      this.ruleForm = row;
      this.disabled = false;
      this.dialogVisible = true;
    },
    // 查看
    handleCheck(row) {
      this.title = "随访记录详细信息";
      this.disabled = true;
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    // 获取数据
    async getList() {
      let res = await getVisits(this.queryParams);
      this.tableData = res.data.list || [];
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style lang="scss" scoped>
.wid {
  width: 100%;
}
</style>
